package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Likes;
import cn.wolfcode.trip.base.mapper.LikesMapper;
import cn.wolfcode.trip.base.service.ILikesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Date;

@Service
public class LikesServiceImpl implements ILikesService {
    @Autowired
    private LikesMapper likesMapper;
    @Override
    public List<Likes> selectLikesByUserId(Long id) {
        return likesMapper.selectLikesByUserId(id);
    }

    @Override
    public Integer selectUnRead(Long id) {

        return likesMapper.selectUnRead(id);
    }

    @Override
    public void changeState(Long travelId) {
        likesMapper.changeState(travelId);
    }

    @Override
    public Likes selectLikesByUserId(Long userId, Long contentId) {
       Likes likes=likesMapper.selectState(userId,contentId);
        return likes;
    }

    @Override
    public void save(Long userId, Long contentId) {
        //通过页面传入的两个id 查询是否有点赞记录,如果有点赞记录,删除,如果没有,保存记录
        Likes likes=likesMapper.selectState(userId,contentId);
        if(likes==null){
            //做保存操作
            Likes likes1 = new Likes();
            likes1.setContentId(contentId);
            likes1.setUserId(userId);
            likes1.setState(false);
            likes1.setTime(new Date());
            likesMapper.insert(likes1);
        }else if(likes !=null){
            //删除这条点赞状态
            likesMapper.deleteByP2Id(userId,contentId);
        }

    }



    @Override
    public void delete(Long userId, Long contentId) {
        //先维护中间表关系,删除中间表的点赞记录

        likesMapper.deleteByP2Id(userId,contentId);
    }
}
